/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.integrationpath.mengine.service.GenericManager;
import com.integrationpath.mengine.model.Category;
import com.integrationpath.mengine.webapp.action.BaseAction;

import java.util.List;

public class CategoryAction extends BaseAction implements Preparable {
    private GenericManager<Category, Integer> categoryManager;
    private List categories;
    private Category category;
    private Integer  id;

    public void setCategoryManager(GenericManager<Category, Integer> categoryManager) {
        this.categoryManager = categoryManager;
    }

    public List getCategories() {
        return categories;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String categoryId = getRequest().getParameter("category.id");
            if (categoryId != null && !categoryId.equals("")) {
                category = categoryManager.get(new Integer(categoryId));
            }
        }
    }

    public String list() {
        categories = categoryManager.getAll();
        return SUCCESS;
    }

    public void setId(Integer  id) {
        this. id =  id;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public String delete() {
        categoryManager.remove(category.getId());
        saveMessage(getText("category.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            category = categoryManager.get(id);
        } else {
            category = new Category();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (category.getId() == null);

        categoryManager.save(category);

        String key = (isNew) ? "category.added" : "category.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}